package com.feisi.controller;

import com.feisi.domain.Focus;
import com.feisi.domain.User;
import com.feisi.service.FocusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/focus")
public class FocusController {

    @Autowired
    private FocusService focusService;

    /**
     * 新增关注
     * 需要前端传一个json格式的Focus对象过来
     *
     * @param focus
     */
    @RequestMapping("/saveFocus")
    public @ResponseBody
    String saveFocus(Focus focus) {
        System.out.println("保存的focus" + focus);
        Integer byUidAndBeid = focusService.findByUidAndBeid(focus);
        if (byUidAndBeid==0){
            System.out.println("hahhahahhahaha");
            System.out.println(byUidAndBeid);
            focusService.saveFocus(focus);
        }

        return "success";
    }


    /**
     * 删除关注
     *
     * @param focus
     * @return
     */
    @RequestMapping("/deleteFocus")
    public @ResponseBody
    String deleteFocus(Focus focus) {
        focusService.deleteFocus(focus);
        return "success";
    }


    /**
     * 查询我的关注
     *
     * @param userid
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/findByUserid")
    public @ResponseBody
    Map<String, Object> findByUserid(Integer userid, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {

        List<User> users = focusService.findByUserid(userid, (page - 1) * limit, limit);
        Integer count = focusService.FocusTotalByUserid(userid);
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println("findByUserid的users为" + users);
        System.out.println("count为" + count);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", users);
        return map;
    }
}
